package com.c5c7.servlet;


import com.c5c7.bean.NoticeBean;
import com.c5c7.dao.NoticeDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 1. 接收post网址 /newsAdd.jsp
 * 2. 接收到传递过来的新闻参数 新闻标题 新闻内容
 * 3。 完成新闻的添加
 * 4. 跳转到其他页面
 */

@WebServlet("/noticeEdit.jsp")
public class NoticeUpdateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user = (String) req.getSession().getAttribute("User"); // 获取会话
        if (user == null || user.equals("")) {
            resp.sendRedirect("login.jsp");
            return;
        }
        req.setCharacterEncoding("utf-8");
        String noticeTitle = req.getParameter("upd_NoticeTitle");
        String noticeContent = req.getParameter("upd_NoticeContent");
        int noticeId = Integer.parseInt(req.getParameter("noticeId"));
        NoticeBean noticeBean = new NoticeBean();
        noticeBean.setNoticeId(noticeId);
        noticeBean.setNoticeTitle(noticeTitle);
        noticeBean.setNoticeContent(noticeContent);
        noticeBean.setAdminName("admin");
        NoticeDao noticeDao = new NoticeDao();
        int result = noticeDao.update(noticeBean);
        if (result > 0) {
            resp.sendRedirect("notice.jsp");
        }else {
            req.setAttribute("msg","更新失败");
            req.setAttribute("url","notice.jsp");
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }

    }
}
